import store, { AppDispatch } from '@/store/index';
import {
  addCommentQuery,
  cancelIsLikingAPI,
  getCommentsAPI,
  isLikingAPI,
} from '@/api/comment';
import { addCommentAPI } from './../../api/comment';
import { ArticleDetailCommentType } from '../reducer/comment';

export function getCommentActionCreator(params: {
  type: string;
  source: string;
}) {
  return async (dispatch: AppDispatch) => {
    const res = await getCommentsAPI(params);
    // console.log(8, res);
    dispatch({
      type: 'comment/saveComments',
      payload: res.data,
    });
  };
}

export function addCommentActionCreator(data: addCommentQuery) {
  return async (dispatch: AppDispatch) => {
    const res = await addCommentAPI(data);
    // console.log(24, res);
    // 添加评论
    dispatch({
      type: 'comment/addComments',
      payload: res.data.new_obj,
    });
  };
}

// 修改评论数
export function updateComm_countActionCreator() {
  return {
    type: 'article/changeArticleDetail',
  };
}

// 点赞评论和取消点赞评论
export function changeCommentDetailActionCreator(id: any, isliking: any) {
  return async (dispatch: AppDispatch) => {
    if (isliking) {
      await isLikingAPI(id);
      dispatch({
        type: 'comment/changeCommentsDetail',
        payload: { id, isliking },
      });
    } else {
      await cancelIsLikingAPI(id);
      dispatch({
        type: 'comment/changeCommentsDetail',
        payload: { id, isliking },
      });
    }
  };
}
